package com.wealth.vote.web.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wealth.member.entity.Membership;
import com.wealth.member.service.IMembershipService;
import com.wealth.system.web.controller.BaseController;
import com.wealth.vote.entity.CooperativeArea;
import com.wealth.vote.entity.CooperativeEmp;
import com.wealth.vote.entity.CooperativeRecord;
import com.wealth.vote.service.ICooperativeAreaService;
import com.wealth.vote.service.ICooperativeEmpService;
import com.wealth.vote.service.ICooperativeRecordService;
import com.wealth.vote.service.VoteService;
import com.wealth.vote.vo.EmpVO;

import iaas.utils.ResultObject;

@Controller
@RequestMapping(value="/manage/vote")
public class VoteController extends BaseController{
	Logger logger = Logger.getLogger(VoteController.class);
	@Autowired
	private ICooperativeAreaService areaService;
	@Autowired
	private ICooperativeEmpService cooperativeEmpService;
	@Autowired
	private ICooperativeRecordService cooperativeRecordService;
	@Autowired
	private IMembershipService iMembershipService;
	@Autowired
	private VoteService voteService;
	/**活动首页*/
	@RequestMapping(value="/one")
	public String toIndexOne(Model  model,HttpServletRequest request){
		try {
			String flag= request.getParameter("flag");
			if(StringUtils.isNotBlank(flag)){
				model.addAttribute("flag", 0);
			}
			List<CooperativeArea> areaList = areaService.getAllArea();
			model.addAttribute("areaList", areaList);
			return "votes/indexOne";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**员工详情*/
	@RequestMapping(value="/empInfo")
	@ResponseBody
	public ResultObject toEmpInfov(@RequestParam String empNo){
		try {
			CooperativeEmp emp =cooperativeEmpService.getEmpByEmpNo(empNo);
			if(emp==null){
				return new ResultObject("111", "没有员工", null);
			}else{
				return new ResultObject("100", "找到员工", emp);
			}
		} catch (Exception e) {
			logger.error(e);
			return new ResultObject("500", "查询错误", null);
		}
	}
	/**员工详情*/
	@RequestMapping(value="/empInfo/{id}")
	public String toEmpInfo(@PathVariable Long id,Model model){
		try {
			CooperativeEmp emp =cooperativeEmpService.get(id);
			List<CooperativeEmp> empList = cooperativeEmpService.getEmpByStatus();
			int ranking = -1;
			if(empList.size()>0){
				for (int i = 0; i <empList.size(); i++) {
					if(empList.get(i).getId()==emp.getId()){
						ranking=i+1;
					}
				}
			}
			EmpVO vo = new EmpVO();
			vo.setEmp(emp);
			vo.setRanking(ranking);
			model.addAttribute("emp", vo);
			return "votes/empInfo";
		} catch  (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**进入员工列表页面*/
	@RequestMapping(value="/empList/{cooperId}")
	public String toEmpList(@PathVariable Long cooperId,Model model){
		try {
			model.addAttribute("cooperId", cooperId);
			return "votes/empList";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**查询员工列表数据*/
	@RequestMapping(value="/empListData/{cooperId}")
	@ResponseBody
	public ResultObject getEmpListData(@PathVariable Long cooperId,@RequestParam(value="pageNumber",defaultValue="1") Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="4") Integer pageSize){
		try {
			return  cooperativeEmpService.getEmpPageData(pageSize,pageNumber,cooperId);
		} catch (Exception e) {
			logger.error(e);
			return new ResultObject("500", "加载失败",null);
		}
	}
	/**进入员工排行榜页面*/
	@RequestMapping(value="/empRanking")
	public String toEmpRanking(Model model){
		try {
			ResultObject result = cooperativeEmpService.getEmpPageData(10,1,null);
			model.addAttribute("empList", result.getResult());
			return "votes/empRanking";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**员工排行榜加载更多数据*/
	@RequestMapping(value="/empRankingMore")
	@ResponseBody
	public ResultObject toEmpRankingMore(@RequestParam(value="pageNumber",defaultValue="1")Integer pageNumber,
			@RequestParam(value="pageSize",defaultValue="10") Integer pageSize){
		try {
 			ResultObject result = cooperativeEmpService.getEmpPageData(pageSize,pageNumber,null);
			return result;
		} catch (Exception e) {
			logger.error(e);
			return  new ResultObject("500", "加载失败",null);
		}
	}
	/**参与方式*/
	@RequestMapping(value="/getWay")
	public String getWay(){
		try {
			return "votes/getway";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**中奖排行*/
	@RequestMapping(value="/luck")
	public String toLuck(Model model){
		try {
			//是否公布排行
			Integer rankFlag = voteService.getRankFlag();
			if(rankFlag==1){
				ResultObject result = cooperativeEmpService.getEmpPageData(10,1,null);
				model.addAttribute("empList", result.getResult());
				model.addAttribute("flag", 1);
			}
			return "votes/luck";
		} catch (Exception e) {
			logger.error(e);
			return "system/500";
		}
	}
	/**投票*/
	@RequestMapping(value="/vote/{id}")
	@ResponseBody
	public ResultObject vote(@PathVariable Long id){
		try {
			Membership ship =iMembershipService.getMemberShipByMemberIdAndCorpId(memberid, corpid);
			String openid = ship.getOpenid();
			List<CooperativeRecord> recordList= cooperativeRecordService.findByOpenid(openid);
			if(recordList.size()>=3){
				return new ResultObject("300","没有投票机会了",null);
			}else{
				CooperativeRecord record  = new CooperativeRecord();
				record.setCreateTime(new Date());
				record.setEmp(new CooperativeEmp(id));
				record.setOpenid(openid);
				cooperativeRecordService.save(record);
				CooperativeEmp emp  = cooperativeEmpService.get(id);
				emp.setVotes(emp.getVotes()+1);
				cooperativeEmpService.save(emp);
				return new ResultObject("200", "投票成功", null);
			}
		} catch (Exception e) {
			logger.error(e);
			return new ResultObject("500", "投票失败", null);
		}
	}
}
